<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('attendance_rules', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 规则基本信息
            $table->string('name', 100);
            $table->string('code', 50)->unique();
            $table->text('description')->nullable();

            // 适用范围
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->json('applicable_employees')->nullable(); // 特定员工列表

            // 工作时间设置
            $table->string('shift_type', 20)->default('regular'); // regular, flexible, rotating, no_check
            $table->time('work_start_time')->nullable();
            $table->time('work_end_time')->nullable();
            $table->decimal('standard_work_hours', 4, 2)->default(8.00);
            $table->boolean('allow_overtime')->default(true);
            $table->decimal('max_overtime_hours', 4, 2)->default(4.00);

            // 弹性工作时间（仅对弹性班次有效）
            $table->time('flexible_start_time')->nullable();
            $table->time('flexible_end_time')->nullable();
            $table->decimal('core_work_hours', 4, 2)->nullable(); // 核心工作时间

            // 迟到早退规则
            $table->integer('late_threshold_minutes')->default(5); // 迟到阈值（分钟）
            $table->integer('early_leave_threshold_minutes')->default(5); // 早退阈值（分钟）
            $table->boolean('allow_makeup_clock')->default(true);
            $table->integer('max_makeup_count_per_month')->default(3);

            // 倒班制设置
            $table->boolean('is_rotating_shift')->default(false);
            $table->json('shift_rotation_pattern')->nullable(); // 轮班模式
            $table->integer('shift_cycle_days')->nullable(); // 轮班周期天数

            // 假期和请假
            $table->boolean('allow_annual_leave')->default(true);
            $table->boolean('allow_sick_leave')->default(true);
            $table->boolean('allow_personal_leave')->default(true);
            $table->integer('annual_leave_days')->default(5);
            $table->boolean('require_leave_approval')->default(true);

            // 特殊规则
            $table->boolean('auto_clock_out')->default(false); // 自动下班
            $table->boolean('allow_remote_work')->default(false);
            $table->json('holidays')->nullable(); // 节假日设置
            $table->json('workdays')->nullable(); // 工作日设置

            // 评分规则
            $table->decimal('late_deduction_rate', 3, 2)->default(0.1); // 迟到扣分率
            $table->decimal('early_leave_deduction_rate', 3, 2)->default(0.1); // 早退扣分率
            $table->decimal('absent_deduction_rate', 3, 2)->default(1.0); // 旷工扣分率

            // 状态和有效期
            $table->string('status', 20)->default('active');
            $table->date('effective_date');
            $table->date('expiration_date')->nullable();
            $table->boolean('is_default')->default(false);

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['department_id']);
            $table->index(['position_id']);
            $table->index(['shift_type']);
            $table->index(['status']);
            $table->index(['is_default']);
            $table->index(['effective_date', 'expiration_date']);
            $table->index(['code']);

            // 外键约束
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('attendance_rules');
    }
};
